package com.zhsj.OkeHomeConsumption.ui.fragment.entering;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hjq.widget.SwitchButton;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.common.MyLazyFragment;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.event.LoginUserpanterEvent;
import com.zhsj.OkeHomeConsumption.other.TimeOutHandler;
import com.zhsj.OkeHomeConsumption.ui.activity.HomeActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.LoginActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.WebActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.CityPartnerActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.EnteringActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.ReviewEnterActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.ReviewPartnerActivity;
import com.zhsj.OkeHomeConsumption.ui.fragment.entering.mvp.EnterPresenter;
import com.zhsj.OkeHomeConsumption.ui.fragment.entering.mvp.EnterUIInterFace;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.LogUtils;
import com.zhsj.OkeHomeConsumption.utils.SharedPreferanceUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGABanner;

import static com.zhsj.OkeHomeConsumption.common.Constants.SHARED_PERFERENCE_USERINFO;

/**
 * author : Xingdx
 * <p>
 * time   : 2018/10/18
 * desc   :商务合作
 */
public final class EnterActivity extends MyActivity
        implements SwitchButton.OnCheckedChangeListener, EnterUIInterFace {

    Unbinder unbinder;
    @BindView(R.id.chengshihehuoren)
    LinearLayout chengshihehuoren;
    @BindView(R.id.chenghsi1)
    LinearLayout chenghsi1;
    @BindView(R.id.shangjia)
    TextView shangjia;
    @BindView(R.id.chengshiheuoren)
    TextView chengshiheuoren;
    FormalUserInfo user = null;
    private EnterPresenter minePersonPresenter;
    private SharedPreferanceUtils mSharePreferanceUtils = null;
    private BGABanner bannerGuideContent;
    /**
     * @Fields topurlArr : TODO banner部url标题集合
     */
    private LinkedHashMap<String, String> topUrlTitleArr = null;
    //图片集合
    private ArrayList<String> imageUrlList = null;
    private ArrayList<String> webUrlList = null;
    private ArrayList<Integer> imagemodle = null;
    private List<String> mBannerTitleLists = null;
    public static EnterActivity newInstance() {
        return new EnterActivity();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_test_b;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_test_b_title;
    }

    @Override
    protected void initView() {
        mSharePreferanceUtils = new SharedPreferanceUtils(getActivity());
        minePersonPresenter = new EnterPresenter(getActivity(),this);
        bannerGuideContent = findViewById(R.id.banner_guide_content);
        minePersonPresenter.IndexBannerGET(17);
    }


    @Override
    protected void initData() {
        user= Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        if (user != null) {
            minePersonPresenter.DetailGet();
        }
        bannerGuideContent.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                startActivity(new Intent(getActivity(), WebActivity.class).putExtra("url", webUrlList.get(position)));
//                Toast.makeText(banner.getContext(), "点击了" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

    /**
     * {@link SwitchButton.OnCheckedChangeListener}
     */

    @Override
    public void onCheckedChanged(SwitchButton button, boolean isChecked) {
        toast(isChecked);
    }





    @OnClick({R.id.chenghsi1, R.id.chengshihehuoren})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chenghsi1:
                if (user != null) {
                    if(user.getLifeUserDTO().getLifeType()==0){
                        if (user.getLifeUserDTO().getApplyCopartnerType() != 0) {
                            if (user.getLifeUserDTO().getApplyCopartnerType()==1) {
                                startActivityForResult(new Intent(getActivity(), ReviewEnterActivity.class),2);
                            }
                        } else {
                            startActivityForResult(new Intent(getActivity(), EnteringActivity.class),0);//商家入驻
                        }
                    }else{
                        startActivityForResult(new Intent(getActivity(), EnteringActivity.class),0);//商家入驻
                    }

                }else{
                    startActivityForResult(new Intent(getActivity(), LoginActivity.class),4);
                }


                break;
            case R.id.chengshihehuoren:
                if (user != null) {
                    if(user.getLifeUserDTO().getLifeType()==0){
                        if (user.getLifeUserDTO().getApplyCopartnerType() != 0) {
                            if (user.getLifeUserDTO().getApplyCopartnerType()==2) {
                                startActivityForResult(new Intent(getActivity(), ReviewPartnerActivity.class),3);
                            }
                        } else {
                            startActivityForResult(new Intent(getActivity(), CityPartnerActivity.class),1);//城市合伙人
                        }
                    }else{
                        startActivityForResult(new Intent(getActivity(), CityPartnerActivity.class),1);//城市合伙人
                    }
                }else{
                    startActivity(LoginActivity.class);
                }

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 0:
                initData();
                break;
            case 1:
                initData();
                break;
            case 2:
                initData();
                break;
            case 3:
                initData();
                break;
            case 4:
                //登陆页面返回
                initData();
                break;
        }
    }

    @Override
    public void DetailGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject dataLoginobj = jsonOBJ.optJSONObject("data");
                user = JsonUtils.object(dataLoginobj.toString(), FormalUserInfo.class);
                Hawk.put(Constants.SHARED_PERFERENCE_USERINFO, user);
                if (user != null) {
                    switch (user.getLifeUserDTO().getLifeType()) {
                        case 0:
                            if (user.getLifeUserDTO().getApplyCopartnerType() != 0) {
                                if (user.getLifeUserDTO().getApplyCopartnerType()==1) {
                                    chengshihehuoren.setEnabled(false);
                                    shangjia.setText("点击查看");
                                    chengshiheuoren.setText("");
                                } else {
                                    chenghsi1.setEnabled(false);
                                    shangjia.setText("");
                                    chengshiheuoren.setText("点击查看");
                                }
                            }
                            break;
                    }
                }
            } else {
                if (!message.equals("null")) {
                    toast("message");
                    TimeOutHandler.pDialogUtils.dismiss();
                }
            }
        } catch (JSONException ex) {
            TimeOutHandler.pDialogUtils.dismiss();
        }
    }

    @Override
    public void IndexBannerGET(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                // 首页Banner数据
                JSONArray optJSONArray = jsonOBJ.optJSONArray("data");
                topUrlTitleArr = new LinkedHashMap<String, String>();
                imageUrlList = new ArrayList<String>();
                webUrlList = new ArrayList<>();
                imagemodle = new ArrayList<>();
                mBannerTitleLists = new ArrayList<String>();
                // 首页数据
                for (int i = 0; i < optJSONArray.length(); i++) {
                    final JSONArray imgPathArray = optJSONArray.optJSONObject(i).optJSONArray("imgPath");
                    if (!imageUrlList.contains(imgPathArray.optString(0))) {
                        imageUrlList.add(imgPathArray.optString(0));
                    }
                    if (!topUrlTitleArr.containsKey(optJSONArray.optJSONObject(i).optString("title")) &&
                            !topUrlTitleArr.containsValue(optJSONArray.optJSONObject(i).optString("url"))) {
                        topUrlTitleArr.put(optJSONArray.optJSONObject(i).optString("title"),
                                optJSONArray.optJSONObject(i).optString("url"));
                        mBannerTitleLists.add(optJSONArray.optJSONObject(i).optString("title"));
                        imagemodle.add(optJSONArray.optJSONObject(i).optInt("module"));
                        webUrlList.add(optJSONArray.optJSONObject(i).optString("url"));
                    }

                }
                //设置图片集合
                bannerGuideContent.setAdapter(new BGABanner.Adapter<ImageView, String>() {
                    @Override
                    public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                        Glide.with(getActivity())
                                .load(model)
//                                .placeholder(R.drawable.homebanneryi)
                                .error(R.drawable.homebannerer)
                                .centerCrop()
                                .dontAnimate()
                                .into(itemView);
                    }
                });

                bannerGuideContent.setData(imageUrlList, mBannerTitleLists);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                }
            } else {
                if (!message.equals("null")) {
                }

            }
        } catch (Exception ex) {
            LogUtils.e("IndexFragemnt banner", "数据获取异常 ");
        }


    }
}